import { pcaa } from 'area-data'

/**
 * 省市区
 */
export default class Area {
  /**
   * 构造器
   * @param express
   */
  constructor() {
    let arr = []
    const province = pcaa['86']
    Object.keys(province).map(key => {
      arr.push({ id: key, text: province[key], pid: '86' });
      const city = pcaa[key];
      Object.keys(city).map(key2 => {
        arr.push({ id: key2, text: city[key2], pid: key });
        const qu = pcaa[key2];
        Object.keys(qu).map(key3 => {
          arr.push({ id: key3, text: qu[key3], pid: key2 });
        })
      })
    })
    this.all = arr;
  }

  get pca() {
    return this.all;
  }

  getCode(text) {
    if (!text || text.length == 0) {
      return ''
    }
    for (let item of this.all) {
      if (item.text === text) {
        return item.id;
      }
    }
  }

  getText(code,status) {
    if (!code || code.length == 0) {
      return ''
    }
    if(!status){
      let arr = []
      let arr2 = []
      this.getAreaBycode(code, arr,arr2);
      return arr.join('/')
    }else{
      let arr = []
      let arr2 = []
      this.getAreaBycode(code, arr,arr2);
      return {cities:arr.join('/'),codes:arr2.join('/')}
    }
  }

  getRealCode(code) {
    let arr = []
    this.getPcode(code, arr)
    return arr;
  }

  getPcode(id, arr) {
    for (let item of this.all) {
      if (item.id === id) {
        arr.unshift(id)
        if (item.pid != '86') {
          this.getPcode(item.pid, arr)
        }
      }
    }
  }

  getAreaBycode(code, arr,arr2) {
    //console.log("this.all.length",this.all)
    if(arr.length ==3){
      return
    }else{
      for (let item of this.all) {
        if (item.id === code) {
          arr.unshift(item.text);
          arr2.unshift(item.id)
          this.getAreaBycode(item.pid, arr,arr2)
        }
      }
    }
  }
  getAreaCodeByText(text, arr) {
    let p_province=[];
    let province = text.split("/")[0];
    let p_city=[];
    let P_ID="";
    let city = text.split("/")[1];
    let c_area=[];
    let C_ID="";
    let area = text.split("/")[2];
    let A_ID="";
    let pro_data = pcaa['86'];
    if (pro_data) {
      for (let key in pro_data) {
        if (pro_data.hasOwnProperty(key)) {
           p_province.push({ value: key, label: pro_data[key], })
        }
      }
    }
    for (let item of p_province) {
      if (item.label === province) {
         P_ID=item.value;
      }
    }
    let p_data = pcaa[P_ID]
    if (p_data) {
      for (let key in p_data) {
        if (p_data.hasOwnProperty(key)) {
           p_city.push({ value: key, label: p_data[key], })
        }
      }
    }

    for (let item of p_city) {
      if (item.label === city) {
         C_ID=item.value;
      }
    }
    let c_data = pcaa[C_ID]
    if (c_data) {
      for (let key in c_data) {
        if (c_data.hasOwnProperty(key)) {
           c_area.push({ value: key, label: c_data[key], })
        }
      }
    }
    for (let item of c_area) {
      if (item.label === area) {
         A_ID=item.value;
         return A_ID
      }
    }
  }

}